<template>
  <el-table :border="true" :data="drugStockList" :header-cell-style="$headerCellStyle" :stripe="true" height="450"
            highlight-current-row>
    <el-table-column :align="'center'" fixed="left" header-align="center" label="序号" prop="rowIndex"
                     width="50">
      <template #default="scope">
        {{ scope.$index + 1 }}
      </template>
    </el-table-column>
    <el-table-column :align="'left'" :show-overflow-tooltip="true" fixed="left" header-align="center" label="药品名称"
                     prop="drugName">
      <template #default="scope">
        <span class="fz14 fw-bold">{{ scope.row.drugName }}</span>
      </template>
    </el-table-column>
    <el-table-column :align="'left'" :show-overflow-tooltip="true" fixed="left" label="生产商" prop="firmName"
                     width="240px"/>
    <el-table-column :align="'right'" :show-overflow-tooltip="true" label="包装规格" prop="packageSpec"
                     width="180px">
      <template #default="scope">
        <span class="ys-css">{{ scope.row.packageSpec }}</span>
      </template>
    </el-table-column>
    <el-table-column :align="'center'" :show-overflow-tooltip="true" label="库存量" prop="minUnits"
                     width="130">
      <template #default="scope">
        <template v-if="scope.row.quantity > 0">
          <template v-if="scope.row.quantityMin > 0">
            <!--小包装-->
            <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.minUnits"
                           style="float: right"/>
            <div style="float: right;margin-right: 5px;font-weight: bold;">
              {{ scope.row.quantityMin }}
            </div>
          </template>
          <!--大包装-->
          <template v-if="scope.row.quantityMax > 0">
            <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.packageUnits"
                           style="float: right"/>
            <div style="float: right;margin-right: 5px;font-weight: bold;">{{ scope.row.quantityMax }}
            </div>
          </template>
        </template>
        <span v-else> <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.packageUnits"
                                     style="float: right"/>
          <div
            style="float: right;margin-right: 5px;font-weight: bold;">0
          </div>
        </span>
      </template>
    </el-table-column>
    <el-table-column :align="'center'" label="上限" prop="actualQuantity" width="130">
      <template #default="scope">
        <template v-if="scope.row.upperQuantity ==0">
          无限制
        </template>
        <template v-else>
          <template v-if="scope.row.minUnits != scope.row.packageUnits">
            <!--小包装-->
            <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.minUnits"
                           style="float: right"/>
            <div style="float: right;margin-right: 5px;font-weight: bold;">
              <span>{{ scope.row.upperQuantityMinActual }}</span>
            </div>
            <!--大包装-->
            <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.packageUnits"
                           style="float: right"/>
            <div style="float: right;margin-right: 5px;font-weight: bold;">
              <span>{{ scope.row.upperQuantityMaxActual }}</span>
            </div>
          </template>
          <template v-else>
            <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.packageUnits"
                           style="float: right"/>
            <div style="float: right;margin-right: 5px;font-weight: bold;">
              <span>{{ scope.row.upperQuantityMinActual }}</span>
            </div>
          </template>
        </template>
      </template>
    </el-table-column>
    <el-table-column :align="'center'" label="下限" prop="actualQuantity" width="130">
      <template #default="scope">
        <template v-if="scope.row.lowerQuantity ==0">
          无限制
        </template>
        <template v-else>
          <template v-if="scope.row.minUnits != scope.row.packageUnits">
            <!--小包装-->
            <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.minUnits"
                           style="float: right"/>
            <div style="float: right;margin-right: 5px;font-weight: bold;">
              <span>{{ scope.row.lowerQuantityMinActual }}</span>
            </div>
            <!--大包装-->
            <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.packageUnits"
                           style="float: right"/>
            <div style="float: right;margin-right: 5px;font-weight: bold;">
              <span>{{ scope.row.lowerQuantityMaxActual }}</span>
            </div>
          </template>
          <template v-else>
            <dict-data-tag :options="dictDataMap['spec_unit']" :value="scope.row.packageUnits"
                           style="float: right"/>
            <div style="float: right;margin-right: 5px;font-weight: bold;">
              <span>{{ scope.row.lowerQuantityMinActual }}</span>
            </div>
          </template>
        </template>
      </template>
    </el-table-column>
  </el-table>
</template>

<script lang="ts" setup>
import {useUserStoreHook} from "@/store/modules/user.ts";
import {useDictStoreHook} from "@/store/modules/dict.ts";
import {listDrugStorageProfile} from "@/api/drug/drugStorageProfile.ts";

const dictStore = useDictStoreHook();
const dictDataMap = ref(dictStore.isTypeGetData('spec_unit', 'sys_yes_no'))
const useUserStore = useUserStoreHook();
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo

const {proxy} = getCurrentInstance() as any
const currentSelectDept = proxy.$currentSelectDept

const drugStockList = ref<any>([])
const props = defineProps({
  selectType: {
    type: String,
    default: ""
  },
  storageId: {
    type: String,
    default: ""
  }
})

const queryParams = ref<any>({
  storageId: props.storageId,
  orgId: userInfo.orgId,
  selectType: props.selectType,
})

watch(() => props.storageId, (now, _old) => {
  queryParams.value.storageId = now
  getList()
})

/** 查询药品库存列表 */
function getList() {
  if (!queryParams.value.orgId) {
    return ElMessage.error(proxy.$OrgIdIsEmpty)
  }
  listDrugStorageProfile(queryParams.value).then((response: any) => {
    drugStockList.value = response.data;
    drugStockList.value.forEach((item: any) => {
      // 现有库存量
      item.quantityMax = Math.floor(item.quantity / item.packageNum)
      item.quantityMin = item.quantity % item.packageNum

      // 最上线转换的大包装数量
      item.upperQuantityMax = Math.floor(item.upperQuantity / item.packageNum)
      item.upperQuantityMin = item.upperQuantity % item.packageNum


      // 最下线转换的大包装数量
      item.lowerQuantityMax = Math.floor(item.lowerQuantity / item.packageNum)
      item.lowerQuantityMin = item.lowerQuantity % item.packageNum
      // 设置最上线和最小线数据
      if (item.lowerQuantity > 0) {
        // 数量/包装数量 的除数
        const bigNum = Math.floor(item.lowerQuantity / item.packageNum)
        // 数量/包装数量 的余数
        const minNum = item.lowerQuantity % item.packageNum
        item.lowerQuantityMaxActual = bigNum
        item.lowerQuantityMinActual = minNum
        if (item.packageUnits === item.minUnits && minNum <= 0) {
          item.lowerQuantityMinActual = bigNum
        }
      }
      if (item.upperQuantity > 0) {
        // 数量/包装数量 的除数
        const bigNum = Math.floor(item.upperQuantity / item.packageNum)
        // 数量/包装数量 的余数
        const minNum = item.upperQuantity % item.packageNum
        item.upperQuantityMaxActual = bigNum
        item.upperQuantityMinActual = minNum
        if (item.packageUnits === item.minUnits && minNum <= 0) {
          item.upperQuantityMinActual = bigNum
        }
      }
    })
  });
}

defineExpose({
  getList
})
</script>

<style scoped>
:deep(.el-table__body tr.current-row>td) {
  background-color: #e9e9e9 !important;
  color: #5f5f5f;
}
</style>
